package com.ww.java8.fp.stream.inaction.ch05;

import java.util.Optional;

import com.ww.java8.fp.stream.inaction.ch04.Dish;

public class Finding {

	public static void main(String[] args) {
		if (isVegetarianFriendlyMenu()) {
			System.out.println("蔬菜友好型");
		}
		System.out.println("--------------");
		
		System.out.println(isHealthyMenu());
		System.out.println("--------------");
		
		System.out.println(isHealthyMenu2());
		System.out.println("--------------");
		
		Optional<Dish> dish = findVegetarianDish();
		dish.ifPresent(d -> System.out.println(d.getName()));
	}
	
	/**
	 * anyMatch
	 * @return
	 */
	private static boolean isVegetarianFriendlyMenu() {
		return Dish.menu.stream().anyMatch(Dish::isVegetarian);
	}
	
	/**
	 * allMatch
	 * @return
	 */
	private static boolean isHealthyMenu() {
		return Dish.menu.stream().allMatch(d -> d.getCalories() < 1000);
	}
	
	/**
	 * noneMatch
	 * @return
	 */
	private static boolean isHealthyMenu2() {
		return Dish.menu.stream().noneMatch(d -> d.getCalories() >= 1000);
	}
	
	/**
	 * findAny
	 * @return
	 */
	private static Optional<Dish> findVegetarianDish() {
		return Dish.menu.stream().filter(Dish::isVegetarian).findAny();
	}
}
